package com.example.myaccount.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.example.myaccount.R;
import com.example.myaccount.fragment.AddIncomeFragment;
import com.example.myaccount.fragment.AddOutcomeFragment;
import com.example.myaccount.fragment.MonthlyIncomeFragment;
import com.example.myaccount.fragment.MonthlyOutcomeFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;

public class AnalysisActivity extends AppCompatActivity {

    private static Context context;
    public static Context getContext(){
        return context;
    }
    private static Activity activity;
    public static Activity getActivity(){ return activity;}

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager2 viewPager2;
    private TabLayoutMediator tabLayoutMediator;
    private ArrayList<Fragment> fragments;
    private ArrayList<String> tabs;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_analysis);

        initData();

        initView();
    }

    private void initData() {
        context = this;
        activity = this;

        tabs = new ArrayList<>();
        tabs.add(getString(R.string.add_income));
        tabs.add(getString(R.string.add_outcome));

        fragments = new ArrayList<>();
        fragments.add(new MonthlyOutcomeFragment());
        fragments.add(new MonthlyIncomeFragment());
    }

    private void initView() {
        toolbar = findViewById(R.id.toolbar_add_flow);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tabLayout = findViewById(R.id.tablayout);
        for(int i=0;i<tabs.size();i++){
            tabLayout.addTab(tabLayout.newTab());
            tabLayout.getTabAt(i).setText(tabs.get(i));
        }
        viewPager2 = findViewById(R.id.viewpager2);
        viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragments.get(position);
            }

            @Override
            public int getItemCount() {
                return fragments.size();
            }
        });
        tabLayoutMediator = new TabLayoutMediator(tabLayout,viewPager2,true,true,(tab,position)->tab.setText(tabs.get(position)));
        tabLayoutMediator.attach();
    }
}